//
//  VideoJsModel.swift
//  d11
//
//  Created by mac on 2018/6/4.
//  Copyright © 2018年 Tech_001. All rights reserved.
//
import JavaScriptCore
@objc class SwiftJavaScriptModel: NSObject, SwiftDelegate {
    var _loadView: UIView?
    var isLoad : Bool = false
    func backDetaile() {
       
       // let webview = WebViewController();
             //webview.navigationController?.popViewController(animated: true)
        //webview.backDetaileActivity();
        DispatchQueue.main.async(execute: {
             GlobalInfo.detaileActivityBack=true;
           GlobalInfo.webViewController?.navigationController?.popViewController(animated: true)
            
        })
      
    }
    
    func activity() -> String {
        print("h5调用了")
        //"getAccount('\( UserDefaults.standard.string(forKey: DefaultKey.USER_LOGIN_NAME)! as NSString)' ,'\( UserDefaults.standard.string(forKey: DefaultKey.USER_DOMAIN)! as NSString)')"
        return "得到原生返回参数:\( UserDefaults.standard.string(forKey: DefaultKey.USER_LOGIN_NAME)! as String)----\(UserDefaults.standard.string(forKey: DefaultKey.USER_DOMAIN)! as String)"
    }
    
    func getUserName() -> String {
        print("H5调用了getUserName")
        if(UserHelper.getLoginStatus()){
            return UserDefaults.standard.string(forKey: DefaultKey.USER_LOGIN_NAME) ?? ""
        }else{
            return ""
        }
        
    }
    
    func getMainUrl() -> String {
        return UserDefaults.standard.string(forKey: DefaultKey.USER_DOMAIN) ?? ""
    }
    
    func backUserNameAndUrl() -> String {
        print("js调用原生!!!!")
        return "test"
    }
    func fullScreen() -> Void{
        NotificationCenter.default.post(name: NSNotification.Name("IsChangeFullScreen"), object: self, userInfo: ["mbx":"wbyzt"])
    }
    
    func openActivityDetails(_ activityId:String) -> Void{
        if self.isLoad == true{
            return
        }
        DispatchQueue.main.async {
                    let window: UIWindow  = ((UIApplication.shared.delegate?.window)!)!
                       self._loadView = nil
                       self._loadView = UIHelper.initLoadView2(withMessage: "读取中...", offsetY: 0)
                       self._loadView?.tag = 9009
                   if self._loadView != nil {
                        window.addSubview(self._loadView!)
                   }
        }
        
            self.isLoad = true
              RequestHelper.get(atPath: "/clientsAPIs/myactivity", withCompletion: { data in
                     if let activityInfoArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]]{
                        DispatchQueue.main.async {
                            var isPushList = true
                            for (index,item) in activityInfoArray.enumerated(){
                                let actid = item["actid"] as? String ?? ""
                                if activityId == actid{
                                    let goNext = TabMineActivityDetailViewController.instanceFromStoryboard()!
                                    goNext.activity = item
                                    goNext.activityImage = imageNamed("img_default_banner")
                                    goNext.activityImageHeight = 65
                                    //实现闭包,获取到传递的参数
                                    goNext.listIndex = index
                                    goNext.hidesBottomBarWhenPushed = true
                                   let topVc =  ToolHelper.getTopVC()
                                    isPushList = false
                                    if topVc != nil {
                                        topVc?.navigationController?.pushViewController(goNext, animated: true)
                                    }
                                    let window: UIWindow  = ((UIApplication.shared.delegate?.window)!)!
                                    window.viewWithTag(9009)?.removeFromSuperview()
                                  
                                    break
                                }
                            }
                            if isPushList == true{
                                UIHelper.alertSimpleMessage(about: "读取失败", withDescription: "未成功获取到对应的优惠活动")
                            }
                           
                            let window: UIWindow  = ((UIApplication.shared.delegate?.window)!)!
                            window.viewWithTag(9009)?.removeFromSuperview()
                            
                            self.isLoad = false
                        }
                    }
                   
                   
                }, withError: { error in
                    DispatchQueue.main.async {
                        self.isLoad = false
                        let window: UIWindow  = ((UIApplication.shared.delegate?.window)!)!
                        window.viewWithTag(9009)?.removeFromSuperview()
                       
                        UIHelper.alertSimpleMessage(about: "读取失败", withDescription: "未成功读取活动信息，请稍后重试，\(getErrorString())")
                    }
                })
    }
    var jsContext:JSContext!
}
